package com.smartwebee.android.blespp.hospital;

import android.app.AlertDialog;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.smartwebee.android.blespp.BluetoothLeService;
import com.smartwebee.android.blespp.utils.FileUtils;
import com.smartwebee.android.blespp.utils.HttpLoading;
import com.smartwebee.android.blespp.utils.Utils;
import com.umeng.message.PushAgent;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

public class BaseActivity extends AppCompatActivity {
    public File file;
    protected RequestQueue requestQueue;
    protected String TAG = "BaseActivity";
    protected AlertDialog dialog;
    protected HttpLoading httpLoading;
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == FileUtils.STORAGE_CODE) {
            if (isAllTrue(grantResults)) {
                file = FileUtils.makeFilePath(FileUtils.saveDir, "hsbluetoothdata.txt");
                //初始化清空文件
                Utils.saveToFile(file, new byte[0], false);
            } else {
                if (!shouldShowRequestPermissionRationale(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    Toast.makeText(this, "存储权限被禁用", Toast.LENGTH_LONG).show();
                }
            }
        }
    }

    public boolean isAllTrue(int[] array) {
        for (int i = 0; i < array.length; i++) {
            if (array[i] != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    protected boolean isUseEventBus() {
        return false;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PushAgent.getInstance(this).onAppStart();
        if (isUseEventBus()) {
            EventBus.getDefault().register(this);
        }
    }

    public static IntentFilter makeGattUpdateIntentFilter() {
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
        intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
        intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED);
        intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);
        intentFilter.addAction(BluetoothLeService.ACTION_WRITE_SUCCESSFUL);
        intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_NO_DISCOVERED);
        return intentFilter;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 注销订阅者
        if (isUseEventBus()) {
            EventBus.getDefault().unregister(this);
        }
        if (requestQueue != null) {
            requestQueue.cancelAll(TAG);
        }
    }


    public void showLoading() {
        showLoading("请求中...");
    }

    public void showLoading(String title) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(false);
        httpLoading = new HttpLoading(this);
        httpLoading.setDesc(title);
        httpLoading.getImgClose().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissLoading();
                if (null != httpLoading.getEvent()) {
                    httpLoading.getEvent().clickEvent();
                }
            }
        });
        builder.setView(httpLoading);
        dialog = builder.create();
        dialog.setCancelable(false);
        dialog.show();
        WindowManager m = getWindowManager();
        Display d = m.getDefaultDisplay();  //为获取屏幕宽、高
        android.view.WindowManager.LayoutParams p = dialog.getWindow().getAttributes();  //获取对话框当前的参数值
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        p.width = (int) (d.getWidth() * 0.7);    //宽度设置为屏幕的0.5
        dialog.getWindow().setAttributes(p);
    }

    public void dismissLoading() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                httpLoading.setProgress(0);
            }
        });
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public interface LoadingDismissEvent {
        void clickEvent();
    }

}
